package com.huacai.bloglike.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huacai.common.annotation.Log;
import com.huacai.common.core.controller.BaseController;
import com.huacai.common.core.domain.AjaxResult;
import com.huacai.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.huacai.bloglike.domain.Bloglike;
import com.huacai.bloglike.service.IBloglikeService;
import com.huacai.common.utils.poi.ExcelUtil;
import com.huacai.common.core.page.TableDataInfo;

/**
 * 博客点赞Controller
 *
 * @author lundao
 * @date 2025-09-12
 */
@RestController
@RequestMapping("/huacai/bloglike")
public class BloglikeController extends BaseController
{
    @Autowired
    private IBloglikeService bloglikeService;

    /**
     * 查询博客点赞列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Bloglike bloglike)
    {
        startPage();
        List<Bloglike> list = bloglikeService.selectBloglikeList(bloglike);
        return getDataTable(list);
    }
    /**
     * 根据userId和blogId删除博客点赞
     */
    @DeleteMapping("/deleteBlogLikeByBlogIdAndUserId/{blogId}")
    public AjaxResult deleteBlogLikeByBlogIdAndUserId(@PathVariable String blogId) {
        return toAjax(bloglikeService.deleteBlogLikeByBlogIdAndUserId(blogId));
    }
    /**
     * 导出博客点赞列表
     */

    @Log(title = "博客点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bloglike bloglike)
    {
        List<Bloglike> list = bloglikeService.selectBloglikeList(bloglike);
        ExcelUtil<Bloglike> util = new ExcelUtil<Bloglike>(Bloglike.class);
        util.exportExcel(response, list, "博客点赞数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Bloglike> util = new ExcelUtil<Bloglike>(Bloglike.class);
        util.importTemplateExcel(response, "博客点赞数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "博客点赞", businessType = BusinessType.IMPORT)

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Bloglike> util = new ExcelUtil<Bloglike>(Bloglike.class);
        InputStream inputStream = file.getInputStream();
        List<Bloglike> list = util.importExcel(inputStream );
        inputStream.close();
        int count = bloglikeService.batchInsertBloglike(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取博客点赞详细信息
     */

    @GetMapping(value = "/{bloglikeId}")
    public AjaxResult getInfo(@PathVariable("bloglikeId") String bloglikeId)
    {
        return success(bloglikeService.selectBloglikeByBloglikeId(bloglikeId));
    }

    /**
     * 新增博客点赞
     */

    @Log(title = "博客点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bloglike bloglike)
    {
        return toAjax(bloglikeService.insertBloglike(bloglike));
    }

    /**
     * 修改博客点赞
     */

    @Log(title = "博客点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bloglike bloglike)
    {
        return toAjax(bloglikeService.updateBloglike(bloglike));
    }

    /**
     * 删除博客点赞
     */

    @Log(title = "博客点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{bloglikeIds}")
    public AjaxResult remove(@PathVariable String[] bloglikeIds)
    {
        return toAjax(bloglikeService.deleteBloglikeByBloglikeIds(bloglikeIds));
    }
}
